package com.bgs.eat.memberPrice.service.Impl;

import com.bgs.eat.memberPrice.entity.MemberPriceEntity;
import com.bgs.eat.memberPrice.mapper.MemberPriceMapper;
import com.bgs.eat.memberPrice.service.MemberPriceService;
import com.bgs.eat.memberType.entity.MemberTypeEntity;
import com.bgs.eat.memberType.mapper.MemberTypeMapper;
import com.bgs.eat.page.MyPageHelper;
import com.bgs.eat.page.PageRequest;
import com.bgs.eat.page.PageResult;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.lang.reflect.Member;
import java.util.List;

/**
 * @ClassName MemberPriceServiceImpl
 * @Description TODO 会员价格管理（后端）
 * @Author G胖
 * @Date 2022-10-12 下午 02:58
 **/

@Service
public class MemberPriceServiceImpl implements MemberPriceService {

    @Resource
    MemberPriceMapper memberPriceMapper;

    /**
     * @Author G胖
     * @Description 新增会员价格（后端）
     * @Date 2022-10-12 下午 03:06
     * @Return com.bgs.eat.config.CommonResult

     */
    @Override
    public Integer saveMemberPrice(MemberPriceEntity memberPriceEntity) {
        memberPriceEntity.setDelFlag(0);
        return memberPriceMapper.insertSelective(memberPriceEntity);
    }

    /**
     * @Author G胖
     * @Description 校验会员价格新增修改是否录入某个月份 （后端）
     * @Date 2022-10-12 下午 03:22
     * @Return com.bgs.eat.config.CommonResult

     */
    @Override
    public MemberPriceEntity selectMemberDetailsMonth(Integer month, Integer memberId) {
        MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
        memberPriceEntity.setMemberId(memberId);
        memberPriceEntity.setMonth(month);
        return memberPriceMapper.selectOne(memberPriceEntity);
    }

    /**
     * @Author G胖
     * @Description 会员价格分页全查+模糊查（会员名称）
     * @Date 2022-10-12 下午 03:38
     * @Return com.bgs.eat.page.PageResult

     */
    @Override
    public PageResult getMemberPriceByPage(PageRequest pageRequest, String memberName) {
        MyPageHelper.startPage(pageRequest);
        Example example = new Example(MemberPriceEntity.class);
        example.createCriteria().andLike("memberName","%"+memberName+"%").andEqualTo("delFlag",0);
        PageResult pageResult = MyPageHelper.pageInfo(memberPriceMapper.selectByExample(example));
        return pageResult;
    }

    /**
     * @Author G胖
     * @Description 查询会员价格（前端购买会员界面）
     * @Date 2022-10-12 下午 06:43
     * @Return com.bgs.eat.config.CommonResult

     */
    @Override
    public List<MemberPriceEntity> getMemberPrice(Integer memberId) {
        MemberPriceEntity memberPriceEntity =  new MemberPriceEntity();
        memberPriceEntity.setMemberId(memberId);
        return memberPriceMapper.select(memberPriceEntity);
    }

    @Resource
    MemberTypeMapper memberTypeMapper;

    /**
    * @Author cjl
    * @Description 修改会员价格（后端）
    * @Date  14:50
    * @Return java.lang.Integer
    * @param memberPriceEntity
    */
    @Override
    public Integer updateMemberPrice(MemberPriceEntity memberPriceEntity) {
        Example example = new Example(MemberTypeEntity.class);
        example.createCriteria().andEqualTo("id",memberPriceEntity.getMemberId());
        MemberTypeEntity memberTypeEntity = memberTypeMapper.selectOneByExample(example);
        memberPriceEntity.setMemberName(memberTypeEntity.getMemberName());
        return memberPriceMapper.updateByPrimaryKeySelective(memberPriceEntity);
    }
}
